<html>
<head><meta charset="utf-8"><title>existential return type in trait · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/existential.20return.20type.20in.20trait.html">existential return type in trait</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="236599130"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/existential%20return%20type%20in%20trait/near/236599130" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/existential.20return.20type.20in.20trait.html#236599130">(Apr 29 2021 at 00:05)</a>:</h4>
<p>is there any reason why this couldn't be a feature:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">Error</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">report</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">impl</span><span class="w"> </span><span class="n">fmt</span>::<span class="n">Display</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="o">'</span><span class="nb">_</span><span class="w"></span>
<span class="w">    </span><span class="k">where</span><span class="w"></span>
<span class="w">        </span><span class="bp">Self</span>: <span class="nb">Sized</span> <span class="p">{</span><span class="w"> </span><span class="fm">todo!</span><span class="p">(</span><span class="s">"provide a default impl for printing error and its sources via error trait"</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="236599151"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/existential%20return%20type%20in%20trait/near/236599151" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/existential.20return.20type.20in.20trait.html#236599151">(Apr 29 2021 at 00:05)</a>:</h4>
<p>right now I get "<code>impl Trait</code> not allowed outside of function and inherent method return types"</p>



<a name="236599298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/existential%20return%20type%20in%20trait/near/236599298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/existential.20return.20type.20in.20trait.html#236599298">(Apr 29 2021 at 00:07)</a>:</h4>
<p>basically, treat methods on traits that have a <code>Self: Sized</code> bound like inherent methods, since they don't go into the vtable</p>



<a name="236600596"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/existential%20return%20type%20in%20trait/near/236600596" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/existential.20return.20type.20in.20trait.html#236600596">(Apr 29 2021 at 00:23)</a>:</h4>
<p>I think that will become a feature soon? To allow async fns in traits</p>



<a name="236604457"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/existential%20return%20type%20in%20trait/near/236604457" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/existential.20return.20type.20in.20trait.html#236604457">(Apr 29 2021 at 01:14)</a>:</h4>
<p>I think that's enabled by <a href="https://rust-lang.github.io/rfcs/2515-type_alias_impl_trait.html">RFC 2515</a> / <a href="https://github.com/rust-lang/rust/issues/63063">#63063</a></p>



<a name="236650485"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/existential%20return%20type%20in%20trait/near/236650485" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/existential.20return.20type.20in.20trait.html#236650485">(Apr 29 2021 at 10:25)</a>:</h4>
<p>Yeah, an explicit type alias is a good starting point since it is otherwise unclear whether each implementation uses its own existential type or if the existential is the same for all implementations (obviously the former would be a nice default should the sugar end up added). As to featuring an otherwise object-safe trait, that would still require <code>where Self : Sized</code>-guarded associated types:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">Error</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">Report</span><span class="o">&lt;'</span><span class="nb">__</span><span class="o">&gt;</span><span class="w"> </span>: <span class="nc">Display</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="o">'</span><span class="nb">__</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="k">where</span><span class="w"></span>
<span class="w">        </span><span class="bp">Self</span><span class="w"> </span>: <span class="nb">Sized</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="k">fn</span> <span class="nf">report</span><span class="w"> </span><span class="p">(</span><span class="bp">self</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="nb">_</span> <span class="nc">Self</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span>-&gt; <span class="nc">Self</span>::<span class="n">Report</span><span class="o">&lt;'</span><span class="nb">_</span><span class="o">&gt;</span><span class="w"></span>
<span class="w">    </span><span class="k">where</span><span class="w"></span>
<span class="w">        </span><span class="bp">Self</span><span class="w"> </span>: <span class="nb">Sized</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Fwiw, for the specific case of <code>impl Display</code>, you can return an <code>ImplDisplay&lt;'_, Self&gt;</code> wrapper that implements <code>Display</code> thanks to a <code>report_fmt</code> method</p>



<a name="236680095"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/existential%20return%20type%20in%20trait/near/236680095" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/existential.20return.20type.20in.20trait.html#236680095">(Apr 29 2021 at 14:17)</a>:</h4>
<p>That would require associated type defaults to be introduced backwards compatibility, right?</p>



<a name="236692492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/existential%20return%20type%20in%20trait/near/236692492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/existential.20return.20type.20in.20trait.html#236692492">(Apr 29 2021 at 15:16)</a>:</h4>
<p>yeah, seems like it</p>



<a name="236712881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/existential%20return%20type%20in%20trait/near/236712881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/existential.20return.20type.20in.20trait.html#236712881">(Apr 29 2021 at 17:26)</a>:</h4>
<p>oof, here's hoping those are available soon</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>